package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.QQ;
import vo.User;
import business.QQService;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class QQAction implements Preparable {
	private QQService service;
	private UserService userService;
	private List<QQ> QQs;// 下来显示的所有qq号
	private List<QQ> pageQQs;// 分页显示的qq号
	private ArrayList<String> delIds = new ArrayList<String>();
	private String userName;// 爬取用的帐号
	private String password;// 爬取用的密码
	private String entry;
	private String selectedNum;// 选中的用于爬取数据源的帐号
	private long state;

	// 分页变量
	private int curPage;
	private int totalPage;
	private int pageSize = 2;

	private User curUser;

	public String listQQ() {
		System.out.println("entry:" + entry);
		List<QQ> all = service.getAllQQs();// 显示所有用于爬取内容的qq帐号
		QQs = new ArrayList<QQ>();
		pageQQs = new ArrayList<QQ>();
		List<String> userNames = new ArrayList<String>();
		for (QQ qq : all) {
			if (!userNames.contains(qq.getUserName())) {
				userNames.add(qq.getUserName());
				QQs.add(qq);
			}
		}
		System.out.println("多少个可用qq号:" + QQs.size());
		QQ qq = service.getBySourceId(Integer.parseInt(entry));// 数据源有可使用的爬取帐号
		if (qq != null)
			selectedNum = qq.getUserName();
		else
			selectedNum = "";
		System.out.println("用于爬取的帐号:" + this.selectedNum);

		if (curPage == 0)
			curPage = 1;
		// 分页代码
		int totalCount = QQs.size();
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);
		int count = 0;
		for (QQ tmp : QQs) {
			count++;
			System.out.println("count:" + count);
			if (count <= (curPage - 1) * pageSize)
				continue;
			if (count > curPage * pageSize)
				continue;
			pageQQs.add(tmp);
		}
		return Action.SUCCESS;
	}

	public String deleteQQ() {
		System.out.println("delete qq:");
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted qq ids：" + delIds.get(i));
			long id = Long.parseLong(delIds.get(i));
			service.delete(id);

		}
		return "QQDeleted";
	}

	public String saveQQ() {
		System.out.println("form save qq+" + entry);
		System.out.println("userName:" + userName);
		System.out.println("password:" + password);

		// if qq exist,add
		if (state == 1) {
			QQ qq = new QQ();
			qq.setUserName(userName);
			qq.setPassword(password);
			qq.setSourceId(Integer.parseInt(entry));
			service.addQQ(qq);
		}
		if (state == 2) {
			QQ qq = service.getBySourceId(Integer.parseInt(entry));
			qq.setUserName(userName);
			service.update(qq.getId());
		}

		return "saveQQ";
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		System.out.println("username:"+username);
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public void setQQService(QQService service) {
		this.service = service;
	}

	public void setService(QQService service) {
		this.service = service;
	}

	public List<QQ> getQQs() {
		return QQs;
	}

	public void setQQs(List<QQ> qQs) {
		QQs = qQs;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public String getEntry() {
		return entry;
	}

	public void setEntry(String entry) {
		this.entry = entry;
	}

	public String getSelectedNum() {
		return selectedNum;
	}

	public void setSelectedNum(String selectedNum) {
		this.selectedNum = selectedNum;
	}

	public long getState() {
		return state;
	}

	public void setState(long state) {
		this.state = state;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<QQ> getPageQQs() {
		return pageQQs;
	}

	public void setPageQQs(List<QQ> pageQQs) {
		this.pageQQs = pageQQs;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}